
import { _decorator, Component, Node, dragonBones, Vec3, ProgressBar, tweenUtil, log, Vec2, Tween } from 'cc';
const { ccclass, property } = _decorator;

/**
 * 演员(actor)基类
 * @param hp 血量
 * @param name 名字
 * @param skeleton 骨骼
 * @method attack 攻击方法 子类需要实现
 * @method hit(hp) 受击方法 子类可以不实现
 */
@ccclass('ActorBase')
export class ActorBase extends Component {
    @property(dragonBones.ArmatureDisplay)
    public skeleton: dragonBones.ArmatureDisplay = null;
    @property(ProgressBar)
    public hpBar: ProgressBar = null;
    private _hp: number = 100;
    private HP_MAX: number = 100;
    private _actorName: string = "actor";
    public set hp(hp: number) {
        if (hp >= 0) {
            this._hp = hp;
        } else {
            this._hp = 0;
        }
    }
    public get hp(): number {
        return this._hp;
    }
    public set actorName(name: string) {
        this._actorName = name;
    }
    public get actorName(): string {
        return this._actorName;
    }

    /**
     * 释放普通攻击的方法
     */
    public attacks(args:{
        v3: Vec3,
        attackCompletionCallback:(isInAction:boolean) => any
    }) {}

    public move(args:{
        v3: Vec3,
        attackCompletionCallback:(isInAction:boolean) => any
    }) {
        new Tween(this.node).to(0.5, { position: args.v3 },{
            'onComplete': () =>{
                args.attackCompletionCallback(false)
            }
        }).start();
    }

    /**
     * 被攻击后扣血的方法
     * @param hp 需要扣除的血量 
     */
    public hit(hp: number) {
        this.hp = this.hp - hp;
        this.hpBar.progress = (this._hp / this.HP_MAX);
    }

    /**
     * 设置位置
     * @param v3
     */
    public setLocation(v3: Vec3) {
        this.node.setWorldPosition(v3)
    }
    /**
     * 显示血条
     */
    public showHpBar() {
        this.hpBar.progress = 1;
        this.hpBar.node.active = true;
    }

}

